package info.yaohua.servlet;

import info.yaohua.dao.Ac01Dao;
import info.yaohua.dto.Ac01Dto;
import info.yaohua.util.SessionNull;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UpdatePasswordServlet
 */
@WebServlet("/UpdatePasswordServlet")
public class UpdatePasswordServlet extends HttpServlet {
	String editMessage=null;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdatePasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession  session = request.getSession();
		if (session == null || session.getAttribute("ac01")==null) {
			SessionNull.forwordTo(request, response);
		} else {
			
			String password_new = request.getParameter("password_new");
			String password_old = request.getParameter("password_old");
			if(password_old.equals("") || password_new.equals(""))
			{
				editMessage = "密码不能为空！";
				request.setAttribute("editMessage", editMessage);
				RequestDispatcher view1 = request.getRequestDispatcher("/editPassword.jsp");
				view1.forward(request, response);
				return;
			}
			Ac01Dto ac01 = (Ac01Dto) session.getAttribute("ac01");
			//String aac001 = ac01.getAac001();
			Ac01Dao ac01Dao = new Ac01Dao();
			ac01.setAac005(password_new);
			try {
				
				int success = ac01Dao.doUpdateAac005ByAac001(ac01);
				if(success>0)
				{
					editMessage = "修改成功！";
					request.setAttribute("editMessage", editMessage);
					RequestDispatcher view1 = request.getRequestDispatcher("/editPassword.jsp");
					view1.forward(request, response);
				}
				else
				{
					editMessage = "修改失败！";
					request.setAttribute("editMessage", editMessage);
					RequestDispatcher view1 = request.getRequestDispatcher("/editPassword.jsp");
					view1.forward(request, response);
				}
			} catch (NamingException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
